package com.mars.base.common.exception;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 应用异常基类
 * @author yhaoquan
 *
 */
public class AppException extends RuntimeException {

	private static final long serialVersionUID = 1L;
	
	protected Logger logger = LoggerFactory.getLogger(this.getClass()) ;
	
	/**
	 * 异常描述信息
	 */
	private String msg ;

	public AppException() {
		super();
	}
	
	public AppException(String msgFormat, Object...args) {
		super(String.format(msgFormat, args));
		this.msg = String.format(msgFormat, args);
	}
	
	public AppException logInfo() {
		this.logger.info(this.msg);
		return new AppException(this.msg) ;
	}
	
	public AppException logError() {
		this.logger.error(this.msg);
		return new AppException(this.msg) ;
	}
	public AppException logError(Exception e) {
		return errorMsg(e) ;
	}
	
	public AppException errorMsg(Exception e) {
		this.logger.error(this.msg + "\r\n" + ExceptionUtils.getMessage(e));
		return new AppException(this.msg + " ### " + ExceptionUtils.getRootCauseMessage(e)) ;
	}

	public AppException(String message, Throwable cause) {
		super(message, cause);
	}

	public AppException(Throwable cause) {
		super(cause);
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}
}
